package com.hnevc.foodserver.service;

import com.hnevc.foodserver.bean.News;
import com.hnevc.foodserver.bean.Types;
import com.hnevc.foodserver.dao.NewsDao;
import com.hnevc.foodserver.dao.TypesDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TypesService {
    @Autowired
    TypesDao typesDao;

    public List<Types> list() {
        List<Types> all = typesDao.findAll();
        return all;
    }

    public Types findone(Types types) {
        Types types1 = typesDao.findOne(Example.of(types)).orElse(null);
        return types1;
    }

    public Types add(Types types) {
        Types save = typesDao.save(types);
        return save;
    }

    public Types deleteById(Types types) {
        try {
            typesDao.deleteById(types.getId());
            return types;
        } catch (Exception e) {
            return null;
        }
    }

    public Types update(Types types) {

        Types save = typesDao.save(types);
        return save;
    }

    public Types save(Types types) {
        Types probe = new Types();
        probe.setId(types.getId());
        Types types1 = typesDao.findOne(Example.of(probe)).orElse(null);
        types1.setIcon(types.getIcon());
        Types save = typesDao.save(types1);
        return save;
    }
}
